package cn.kgc.api.controller;

import cn.hutool.extra.pinyin.PinyinUtil;
import cn.kgc.api.entity.Menus;
import cn.kgc.api.form.MenusAddForm;
import cn.kgc.api.form.MenusUpdateForm;
import cn.kgc.api.service.MenusService;
import cn.kgc.api.service.RolesMenusService;
import cn.kgc.common.constant.ResultConstant;
import cn.kgc.common.util.HttpException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * @author kgc
 */
@RestController
@RequestMapping("/menus")
@Api(tags = "菜单模块")
@CrossOrigin
@Slf4j
@Validated
public class MenuController {
    @Resource
    private MenusService menusService;
    @Resource
    private RolesMenusService rolesMenusService;

    @PostMapping("/list")
    @ApiOperation(value = "获得所有菜单信息")
    public List<Menus>  getAllMenus(@RequestBody Map<String, String> params){
        return menusService.getAllMenus(params);
    }

    @GetMapping("/detail/{id}")
    @ApiOperation(value = "获得菜单详细信息")
    public Menus detail(@PathVariable("id") int id){
        return menusService.getById(id);
    }


    @PostMapping("/save")
    @ApiOperation(value = "新增菜单信息")
    public boolean save(@RequestBody MenusAddForm menusAddForm){
        int count=menusService.count(menusAddForm.getParentId(),menusAddForm.getMenuName());
        if(count>0){
            throw new HttpException(ResultConstant.MENU_NAME_REPEAT_ERROR);
        }
        String menuCode = PinyinUtil.getFirstLetter(menusAddForm.getMenuName(), "");
        Menus menus = new Menus();
        BeanUtils.copyProperties(menusAddForm,menus);
        menus.setMenuCode(menuCode);
        return menusService.save(menus);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新菜单信息")
    public boolean update(@RequestBody MenusUpdateForm menusUpdateForm){
        Integer id = menusUpdateForm.getId();
        Menus menus = menusService.getById(id);
        if(!menus.getMenuName().equals(menusUpdateForm.getMenuName())){
            int count=menusService.count(menusUpdateForm.getParentId(),menusUpdateForm.getMenuName());
            if(count>0){
                throw new HttpException(ResultConstant.MENU_NAME_REPEAT_ERROR);
            }
        }
        String menuCode = PinyinUtil.getFirstLetter(menusUpdateForm.getMenuName(), "");
        BeanUtils.copyProperties(menusUpdateForm,menus);
        menus.setMenuCode(menuCode);
        return menusService.updateById(menus);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除菜单信息")
    public boolean delete(@PathVariable int id){
        //菜单如果有子菜单,菜单不能删除
        int count=menusService.count(id,null);
        if(count>0){
            throw new HttpException(ResultConstant.MENU_HAS_CHILD_ERROR);
        }
        //菜单被角色使用,菜单不能删除
        count= rolesMenusService.count(id);
        if(count>0){
            throw new HttpException(ResultConstant.MENU_USED_ERROR);
        }
        return menusService.removeById(id);
    }

}
